package org.aalku.util.classLoaders.maven.exporter;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class ClasspathExporter {

	private static final String DEFAULT_PACKAGING = "jar";
	private static final String POM_EXT = ".pom";

	/**
	 * @param args
	 * @throws IOException
	 * @throws MalformedURLException
	 * @throws SAXException
	 * @throws ParserConfigurationException
	 * @throws XPathExpressionException
	 */
	public static void main(String[] args) throws MalformedURLException,
			IOException, ParserConfigurationException, SAXException,
			XPathExpressionException {
		List<String> cp = new ArrayList<String>();
		for (String entry : System.getProperty("java.class.path").split(
				Pattern.quote(System.getProperty("path.separator")))) {
			Document pom;
			File pomFile = new File(entry.replaceFirst("\\.[^.]*$", POM_EXT));
			if (pomFile.isFile() && pomFile.getName().endsWith(POM_EXT)) {
				InputStream pomIn = pomFile.toURI().toURL().openStream();
				pom = parseXml(pomIn);
			} else {
				pom = null;
			}
			if (pom == null) {
				cp.add("file://"+entry);
			} else {
				XPath xPath = XPathFactory.newInstance().newXPath();
				String groupId = (String) xPath.evaluate(
						"project/groupId/text()", pom, XPathConstants.STRING);
				String artifactId = (String) xPath
						.evaluate("project/artifactId/text()", pom,
								XPathConstants.STRING);
				String version = (String) xPath.evaluate(
						"project/version/text()", pom, XPathConstants.STRING);
				String classifier = (String) xPath
						.evaluate("project/classifier/text()", pom,
								XPathConstants.STRING);
				String packaging = (String) xPath.evaluate(
						"project/packaging/text()", pom, XPathConstants.STRING);
				if (groupId == null || groupId.isEmpty()) {
					groupId = (String) xPath.evaluate(
							"project/parent/groupId/text()", pom,
							XPathConstants.STRING);
				}
				if (version == null || version.isEmpty()) {
					version = (String) xPath.evaluate(
							"project/parent/version/text()", pom,
							XPathConstants.STRING);
				}
				if (packaging == null || packaging.isEmpty()) {
					packaging = DEFAULT_PACKAGING;
				}
				String url = "maven://"
						+ groupId.replace('.', '/') + '/'
						+ artifactId + '/' + version + '/'
						+ artifactId + '-' + version + '.'
						+ packaging;
				cp.add(url);
			}
		}
		PrintWriter w = new PrintWriter("maven-classpath.cfg");
		try {
			for (String l : cp) {
				System.out.println(l);
				w.println(l);
			}
		} finally {
			w.close();
		}
	}

	private static Document parseXml(InputStream in)
			throws ParserConfigurationException, SAXException, IOException {
		DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
		Document doc = dBuilder.parse(in);
		return doc;
	}

}
